package org.NooLab.glue.msgframe.message;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.SocketException;

import org.NooLab.glue.net.sockets.tcp.TcpMessageHandlerFactory;


/**
 * 
 * 
 * 
 */
public class BoardMsgHandler implements BoardMsgHandlerIntf{

	TcpMessageHandlerFactory msgFactory;
	
	ObjectInputStream objInStream;
	long msgid=0;
	String rawmessage="";
	
	
	public BoardMsgHandler( TcpMessageHandlerFactory msgfactory ){
		msgFactory = msgfactory;
		
	}
	
	
	
	public void takeObjectStream( ObjectInputStream objinstream){
		objInStream = objinstream;
		rawmessage="";
	}
	
	
	public String getRawMessageAsString(){
		
		if ( rawmessage.length()==0){
			try {
				if (objInStream != null) {
					rawmessage = (String) objInStream.readObject();
				}

			} catch (java.io.EOFException efx) {
				System.err.println("ServerMsgHandler, EOFException (m): \n"); 
				efx.printStackTrace();
			} catch (SocketException sex) {
				System.err.println("ServerMsgHandler, SocketException (m): " + sex.getMessage());
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				System.err.println("ServerMsgHandler: Data received in unknown format");
			}
		}
		
		return rawmessage;
	}
	
	
	
	
	public String getResponseStringOfBoard(){
		msgid++;
		String responseStr="";
		
		if (rawmessage.contains("<?xml")==false){
			responseStr = "message ("+msgid+") by server time = "+System.currentTimeMillis();
			return responseStr;
		}
		
		if (rawmessage.contains("<register>")==false){
			
		}
		
		
		return responseStr;
	}
	
	public int getActionCode(){
		int code=1;
		
		if ((msgid>=500000000) || (rawmessage.contentEquals("bye"))){
			code = -3;
		}
		return code;
	}



	@Override
	public String getResponseString() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
